package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.cskaoyan.service.DataSourceHolder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/11/11 14:47
 */
@Configuration
@ComponentScan("com.cskaoyan")
public class SpringConfiguration {

    /**
     * 返回值类型：建议写接口类型的返回值
     * 方法名：作为默认的组件id的
     * @Bean的value属性值指定
     * 形参：如果写了形参，默认按照类型从容器中取出组件，给到这个形参
     * 如果在形参写的这个类型的组件在容器中不止一个，额外指定组件id → @Qualifier
     */
    @Bean("ds1")
    public DataSource dataSource1() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cskaoyan_db?useUnicode=true");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }
    @Bean("ds2")
    public DataSource dataSource2() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cskaoyan_db?useUnicode=true");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public DataSourceHolder dataSourceHolder(@Qualifier("ds1") DataSource dataSource) {
        DataSourceHolder dataSourceHolder = new DataSourceHolder();
        dataSourceHolder.setDataSource(dataSource);
        return dataSourceHolder;
    }
}
